using System;
using System.IO;
using System.Security;
#if !SANDBOX
using Microsoft.Win32;
#endif

namespace PS.Practices.Util
{
    public static class EnvironmentUtil
    {
        public static string GetWSSSiteDefFolder()
        {
            string value = GetWSSFolder();
            return Path.Combine(value, @"template\SiteTemplates");
        }
        
        public static string GetWSSFolder()
        {
#if !SANDBOX
            string key = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0";
            string name = "Location";

            string wss14Dir;
            try
            {
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey(key);
                wss14Dir = regKey.GetValue(name) as string;
                regKey.Close();
            }
            catch (SecurityException)
            {
                wss14Dir = String.Empty;
            }
            catch (ArgumentNullException)
            {
                wss14Dir = String.Empty;
            }
            catch (ArgumentException)
            {
                wss14Dir = String.Empty;
            }
            catch (ObjectDisposedException)
            {
                wss14Dir = String.Empty;
            }
            catch (IOException)
            {
                wss14Dir = String.Empty;
            }
            catch (UnauthorizedAccessException)
            {
                wss14Dir = String.Empty;
            }

            return wss14Dir;
#else
            throw new Exception("'GetWSSFolder' method cannot be used in sandbox solution");
#endif
        }
    }
}